package net.wenku8.reader.view.online

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.LocalNavController
import net.wenku8.reader.enums.RouteOnlineRead
import net.wenku8.reader.enums.RouteOnlineReviewList
import net.wenku8.reader.model.book.Volume
import net.wenku8.reader.state.StateReadOnline
import net.wenku8.reader.ui.lite.LiteCardFold
import net.wenku8.reader.ui.lite.LiteLine
import net.wenku8.reader.ui.lite.LitePanel
import net.wenku8.reader.ui.lite.LiteText
import net.wenku8.reader.ui.lite.LiteTextButton
import net.wenku8.reader.ui.lite.LiteTextFold
import net.wenku8.reader.ui.lite.LiteTextLink

@Composable
fun OnlineDetail() {
    val onlineDetailViewModel = viewModel<OnlineDetailViewModel>()
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        onlineDetailViewModel.initData()
    }
    LitePanel(
        hostState = onlineDetailViewModel.hostState,
        title = "小说详情",
        dataLoading = onlineDetailViewModel.dataLoading,
        backNav = true
    ) {
         Column{
            OnlineBookPreview(onlineDetailViewModel.bookPreviewInfo, false)
            LiteTextFold(title = "小说简介", detail = onlineDetailViewModel.bookDesc)
            LiteLine{
                LiteTextButton("推荐本书") { onlineDetailViewModel.bookVote() }
                if (onlineDetailViewModel.inBookcase){
                    LiteTextButton("移出书架"){
                        onlineDetailViewModel.delBookcase()
                    }
                }else{
                    LiteTextButton("加入书架"){
                        onlineDetailViewModel.addBookcase()
                    }
                }
            }
            if (onlineDetailViewModel.hisLoad){
                LiteLine(
                    onClick = {
                        onlineDetailViewModel.toOnlineRead(onlineDetailViewModel.hisChapter)
                        navController.navigate(RouteOnlineRead)
                    }
                ){
                    LiteText("阅读历史：" + onlineDetailViewModel.hisChapter.title)
                }
            }
            LiteLine(
                onClick = {
                    StateReadOnline.bookName.value = onlineDetailViewModel.bookPreviewInfo.title
                    navController.navigate(RouteOnlineReviewList)
                }
            ){
                LiteText("前往书评区")
            }
            LazyColumn{
                items(items = onlineDetailViewModel.volumeList) { volume ->
                    VolumeChapter(volume)
                }
            }
        }
    }
}

@Composable
fun VolumeChapter(volume : Volume){
    val onlineDetailViewModel = viewModel<OnlineDetailViewModel>()
    val navController = LocalNavController.current
    LiteCardFold(
        header = {
            LiteText(text = volume.title?:"")
        }
    ){
        volume.chapterList?.forEach { chapter ->
            LiteTextLink(chapter.title?:"", indent = 28){
                onlineDetailViewModel.toOnlineRead(chapter)
                navController.navigate(RouteOnlineRead)
            }
        }

    }
}

